<?php

class MemberControl extends CommonControl {

    public function __construct() {
        parent::__construct();
        if (!isset($_SESSION['username'])) {
            $this->success('请先登陆', __ROOT__);
        }
        $this->assign_head();
    }

    public function index() {
        //用户的所有提问
        $data = M('user')->query('SELECT *,a.content as con,ca.title as title, a.time as t, count(an.content) as num,a.asid as ask_id FROM hd_ask as a left  join hd_answer as an on a.asid=an.asid join hd_category as ca on ca.cid=a.cid where a.uid=' . $_SESSION['uid'] . ' group by con');
        $this->assign('askData', $data);
        //用户参与回答的问题
        $data = M('user')->query(' select *,an.content as acon,a.answer as ans,an.time as atime, an.asid as askid from hd_answer as an left join hd_ask as a on an.asid=a.asid join hd_category as ca on ca.cid=a.cid where an.uid=' . $_SESSION['uid'] . ' group by acon');
        $this->assign('ansData', $data);
        $this->display();
    }

    //我的提问
    public function my_ask() {
        $data = M('user')->query('SELECT * FROM hd_user  as u join hd_ask as a on u.uid=a.uid join hd_category as c on a.cid=c.cid where a.solve=0 and a.uid=' . $_SESSION['uid']);
        $data2 = M('user')->query('SELECT * FROM hd_user  as u join hd_ask as a on u.uid=a.uid join hd_category as c on a.cid=c.cid where a.solve=1 and a.uid=' . $_SESSION['uid']);
        $this->assign('unsolve', $data);
        $this->assign('solved', $data2);
        $this->display();
    }

    public function my_answer() {
        $data = M('user')->query(' select *,an.content as acon,a.answer as ans,an.time as atime, an.asid as askid from hd_answer as an left join hd_ask as a on an.asid=a.asid join hd_category as ca on ca.cid=a.cid where an.uid=' . $_SESSION['uid'] . ' group by acon');
        $this->assign('ansData', $data);
        $data = M('user')->query(' select *,an.content as acon,ca.title as cate ,a.answer as ans,an.time as atime, an.asid as askid from hd_answer as an left join hd_ask as a on an.asid=a.asid join hd_category as ca on ca.cid=a.cid where an.accept=1 and an.uid=' . $_SESSION['uid'] . ' group by acon');
        // p($data);
        $this->assign('SolvedData', $data);
        $this->display();
    }

    public function my_face() {
        if (!empty($_FILES)) {
            $obj = new Upload();
            $fullPath = $obj->up();
            if (!empty($fullPath)) {
                $fullPath = $fullPath[0]['url'];
                $sql = 'UPDATE hd_user set face="' . $fullPath . '" where uid=' . $_SESSION['uid'];
                if (M('user')->exec($sql)) {
                    $this->error('上传成功！');
                }
            } else {
                $this->error($obj->error);
            }
        }
        $data = M('user')->query('SELECT * FROM hd_user where uid=' . $_SESSION['uid']);
        $this->assign('face', $data[0]);
        $this->display();
    }

    public function my_gold() {
        $this->display();
    }

    public function my_level() {
        $this->display();
    }

}
